package com.zidian.study.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.zidian.study.bean.StudentClassBean;

import java.util.List;

import com.zidian.student.trucks.R;

/**
 * Created by Administrator on 2016/8/24 0024.
 * 蹭课适配器
 */
public class CourseClassAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    private static final int TYPE_ITEM = 0;
    private static final int TYPE_FOOTER = 1;
    private Context context;
    private List<StudentClassBean.DataBean> data;
    private boolean hasMore = true;


    public CourseClassAdapter(Context context, List<StudentClassBean.DataBean> data) {
        this.context = context;
        this.data = data;
    }

    public void setHasMore(boolean hasMore) {
        this.hasMore = hasMore;
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);

        void onItemLongClick(View view, int position);
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public int getItemCount() {
        return data.size() == 0 ? 0 : data.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position + 1 == getItemCount()) {
            return TYPE_FOOTER;
        } else {
            return TYPE_ITEM;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_ITEM) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_course_add, parent,
                    false);
            return new ItemViewHolder(view);
        } else if (viewType == TYPE_FOOTER) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_interaction_ask_foot, parent,
                    false);
            return new FootViewHolder(view);
        }
        return null;
    }

    public interface OnButtonClickListener {

        void onAddClick(String courseId, String teacherName, int position);

        void onDeleteClick(String courseId, String teacherName, int position);

    }

    private OnButtonClickListener onButtonClickListener;

    public void setOnButtonClickListener(OnButtonClickListener onButtonClickListener) {
        this.onButtonClickListener = onButtonClickListener;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ItemViewHolder) {

            ((ItemViewHolder) holder).className.setText(data.get(position).getCourseName());
            ((ItemViewHolder) holder).teacherName.setText(data.get(position).getCourseTeacher());
            ((ItemViewHolder) holder).classTime.setText(data.get(position).getCourseTime());
            ((ItemViewHolder) holder).classPersonNum.setText(data.get(position).getTypeNum());
            if (data.get(position).getCourseType() == 0) {
                ((ItemViewHolder) holder).classAdd.setVisibility(View.VISIBLE);
                ((ItemViewHolder) holder).classDelete.setVisibility(View.GONE);
            } else {
                ((ItemViewHolder) holder).classDelete.setVisibility(View.VISIBLE);
                ((ItemViewHolder) holder).classAdd.setVisibility(View.GONE);
            }


            if (onButtonClickListener != null) {
                ((ItemViewHolder) holder).classAdd.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int position = holder.getLayoutPosition();
                        onButtonClickListener.onAddClick(data.get(position).getCourseId(), data.get(position).getCourseTeacher(), position);
                    }
                });
                ((ItemViewHolder) holder).classDelete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int position = holder.getLayoutPosition();
                        onButtonClickListener.onDeleteClick(data.get(position).getCourseId(), data.get(position).getCourseTeacher(), position);
                    }
                });
            }

            if (onItemClickListener != null) {


            }
        } else if (holder instanceof FootViewHolder) {
            if (hasMore) {
                ((FootViewHolder) holder).progressBar.setVisibility(View.VISIBLE);
                ((FootViewHolder) holder).loadText.setText("正在加载");
            } else {
                ((FootViewHolder) holder).progressBar.setVisibility(View.GONE);
                ((FootViewHolder) holder).loadText.setText("加载完成");
            }
        }
    }


    static class ItemViewHolder extends RecyclerView.ViewHolder {

        TextView className;
        TextView teacherName;
        TextView classTime;
        TextView classPersonNum;
        Button classAdd;
        Button classDelete;


        public ItemViewHolder(View view) {
            super(view);

            className = (TextView) view.findViewById(R.id.class_name);
            teacherName = (TextView) view.findViewById(R.id.teacher_name);
            classTime = (TextView) view.findViewById(R.id.class_time);
            classPersonNum = (TextView) view.findViewById(R.id.class_person_num);
            classAdd = (Button) view.findViewById(R.id.class_add);
            classDelete = (Button) view.findViewById(R.id.class_delete);

        }
    }

    static class FootViewHolder extends RecyclerView.ViewHolder {

        ProgressBar progressBar;
        TextView loadText;

        public FootViewHolder(View view) {
            super(view);
            progressBar = (ProgressBar) view.findViewById(R.id.progressBar);
            loadText = (TextView) view.findViewById(R.id.load_text);
        }
    }


}
